package com.lwb.eduservice.controller.front;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lwb.commonutils.CourseUtilsVo;
import com.lwb.commonutils.JwtUtils;
import com.lwb.commonutils.Row;
import com.lwb.commonutils.UcenterMemberVo;
import com.lwb.eduservice.client.OrderClient;
import com.lwb.eduservice.client.UcenterMemberClient;
import com.lwb.eduservice.entity.EduCourse;
import com.lwb.eduservice.entity.chapter.ChapterVo;
import com.lwb.eduservice.entity.frontvo.CourseFrontVo;
import com.lwb.eduservice.entity.frontvo.CourseWebVo;
import com.lwb.eduservice.entity.vo.CourseInfoVo;
import com.lwb.eduservice.service.EduChapterService;
import com.lwb.eduservice.service.EduCourseService;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import static com.lwb.commonutils.JwtUtils.APP_SECRET;

@RestController
@RequestMapping("/eduservice/coursefront")
public class CourseFrontController {
    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrderClient orderClient;

    //1 条件查询带分页查询课程
    @PostMapping("getFrontCourseList/{page}/{limit}")
    public Row getFrontCourseList(@PathVariable long page, @PathVariable long limit,
                                  @RequestBody(required = false) CourseFrontVo courseFrontVo) {
        Page<EduCourse> pageCourse = new Page<>(page,limit);
        Map<String,Object> map = courseService.getCourseFrontList(pageCourse,courseFrontVo);
        //返回分页所有数据
        return Row.SUCCESS().data(map);
    }

    //2. 查询课程详情
    @PostMapping("getFrontCourseInfo/{courseId}")
    public Row getFrontCourseInfo(@PathVariable String courseId,HttpServletRequest request){
        CourseWebVo courseWebVo = courseService.getFrontCourseInfo(courseId);
        //查询课程章节
        List<ChapterVo> chapterVideoList = chapterService.getChapterVideoById(courseId);
        Boolean buyCourse = orderClient.isBuyCourse(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return Row.SUCCESS().data("chapterVideoList",chapterVideoList).data("courseWebVo",courseWebVo).data("isBuy",buyCourse);
    }

    //3. 根据课程id查询课程信息
    @PostMapping("getCourseInfoByOrder/{courseId}")
    public CourseUtilsVo getCourseInfoByOrder(@PathVariable String courseId){
        CourseWebVo courseInfo = courseService.getFrontCourseInfo(courseId);
        CourseUtilsVo courseUtilsVo = new CourseUtilsVo();
        BeanUtils.copyProperties(courseInfo,courseUtilsVo);
        return courseUtilsVo;
    }
}
